<?php

/**
 * nearnews actions.
 *
 * @package    nfzz
 * @subpackage nearnews
 * @author     Francisco Calderón <fjcalderon@gmail.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class nearnewsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->nf_nearnewss = Doctrine_Core::getTable('NfNearnews')
      ->createQuery('a')
      ->execute();
  }

  public function executeNear(sfWebRequest $request)
  {
    $this->events = Doctrine_Core::getTable('NfNearnews')->getByRadius(sfConfig::get('app_event_radius'), $request->getParameter('lat'), $request->getParameter('lng'), sfConfig::get('app_event_items'));
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->nf_nearnews = Doctrine_Core::getTable('NfNearnews')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->nf_nearnews);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new NfNearnewsForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new NfNearnewsForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($nf_nearnews = Doctrine_Core::getTable('NfNearnews')->find(array($request->getParameter('id'))), sprintf('Object nf_nearnews does not exist (%s).', $request->getParameter('id')));
    $this->form = new NfNearnewsForm($nf_nearnews);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($nf_nearnews = Doctrine_Core::getTable('NfNearnews')->find(array($request->getParameter('id'))), sprintf('Object nf_nearnews does not exist (%s).', $request->getParameter('id')));
    $this->form = new NfNearnewsForm($nf_nearnews);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($nf_nearnews = Doctrine_Core::getTable('NfNearnews')->find(array($request->getParameter('id'))), sprintf('Object nf_nearnews does not exist (%s).', $request->getParameter('id')));
    $nf_nearnews->delete();

    $this->redirect('nearnews/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $nf_nearnews = $form->save();

      $this->redirect('nearnews/edit?id='.$nf_nearnews->getId());
    }
  }
}
